//------------------------------------------------------------------------------
//  <copyright file="PathPlanningAndTrackingModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Robotics.Navigation.Visualization
{
    using System;
    using System.Runtime.Serialization;
    using System.Windows.Media.Imaging;
    using Microsoft.Robotics.Navigation.Localization;
    using Microsoft.Robotics.Navigation.Motion;
    using Microsoft.Robotics.Navigation.Runtime.Motion;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;
    
    /// <summary>
    /// Generates a model for visualization of the path plan.
    /// </summary>
    [DataContract]
    public sealed class PathPlanningAndTrackingModelAgent : ModelAgent<SimpleAgentMessage<PathPlan>, PathPlanningAndTrackingModel>
    {
        /// <summary>
        /// Number of map cell steps ahead to target.
        /// </summary>
        [DataMember(Name = "TargetMapCellsAhead")]
        private readonly int targetMapCellsAhead;

        /// <summary>
        /// Model updating logic.
        /// </summary>
        private PathPlanningModelUpdater updater;

        /// <summary>
        /// Initializes a new instance of the PathPlanningAndTrackingModelAgent class.
        /// </summary>
        /// <param name="name">The name of the agent.</param>
        /// <param name="pathPlanProducerAgent">The name of the path plan producer agent.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The max UI update interval.</param>
        /// <param name="targetStepsAhead">Track ahead distance (in meters).</param>
        public PathPlanningAndTrackingModelAgent(string name, IProducer<SimpleAgentMessage<PathPlan>> pathPlanProducerAgent, int maxUpdateIntervalMilliseconds, int targetStepsAhead)
            : base(name, pathPlanProducerAgent.Name, maxUpdateIntervalMilliseconds)
        {
            this.targetMapCellsAhead = targetStepsAhead;
        }

        /// <summary>
        /// Initialize the agent.
        /// </summary>
        /// <param name="locator">The locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            this.updater = new PathPlanningModelUpdater();
        }

        /// <summary>
        /// Update the model when a new map is received.
        /// </summary>
        /// <param name="state">State containing a path plan.</param>
        /// <returns>True if the model was updated.</returns>
        protected override bool UpdateModel(SimpleAgentMessage<PathPlan> state)
        {
            if (state != null)
            {
                this.updater.UpdateModel(state.State, this.targetMapCellsAhead, (bmp => this.Model.ImageSource = bmp), PathPlanningAndTrackingView.Instance);
                return true;
            }

            return false;
        }
    }
}
